package com.opoc.controller;

import com.opoc.mapper.ReportMapper;
import com.opoc.pojo.Pool;
import com.opoc.pojo.Report;
import com.opoc.pojo.Result;
import com.opoc.service.ReportService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: opoc
 * @description: 检测报告
 * @author: lyx
 * @create: 2023-10-26 14:46
 */

@RestController
@RequestMapping("/admin/report")
public class ReportController {
    @Autowired
    ReportService reportService;

    @Autowired
    ReportMapper reportMapper;

    /**
     * 获取基本信息
     * */
    @GetMapping("/basic")
    public Result getReportInfo(
            @RequestParam(value = "company_id", required = false) String company_id,
            @RequestParam(value = "company_name", required = false) String company_name,
            @RequestParam(value = "contacts", required = false) String contacts
    ) {
        List<Report> data = reportService.getReportInfo(company_id, company_name, contacts);
        return data.isEmpty() ? Result.error("无数据") : Result.success(data);
    }

    /**
     * 添加检测报告
     */
    @PostMapping("/add")
    public Result addReport(@Param("company_id") String company_id,
                            @Param("date") String date,
                            @Param("people") String people,
                            @Param("report_file") String report_file) {
        String result = reportService.addReport(company_id, date, people, report_file);
        return "access".equals(result) ? Result.success() : Result.error(result);

    }



    /**
     * 删除检测报告
     * */
    @DeleteMapping ("/delete")
    public Result deleteReport(@RequestParam("company_ids")List<String>company_ids){
       if (company_ids.isEmpty()){
           return Result.error("is_null");
       }
        String result = reportService.deleteReport(company_ids);
        return "access".equals(result)?Result.success():Result.error(result);

    }

  /**
   * 上传检测报告文件
   * */
  @PostMapping("/upload")
  public Result uploadFile(@RequestParam(value="company_id",required = false) String company_id,
                           @RequestParam(value = "report_file",required = false)MultipartFile report_file){
      if(!reportMapper.validReportById(company_id)){
          return Result.error("企业不存在");
      }
      if(report_file==null){
          return Result.error("未选择上传文件");
      }
      Map<String,String> result=reportService.uploadFile(company_id, report_file);
      return result.isEmpty() ? Result.error("上传失败") : Result.success(result);

  }
  /*
  * 下载检测报告
  * */
  @GetMapping("/download")
public Result downloadFile(@RequestParam(value = "company_id",required = false)String company_id) throws IOException {
      String report_file_url=reportMapper.URLfindId(company_id);
      if(!reportMapper.validReportById(company_id)){
          return Result.error("企业不存在");
      }
      if(report_file_url==null){
          return Result.error("该企业还未上传检测报告");
      }
      else {
      ResponseEntity<byte[]> data= reportService.downloadFile(company_id);
      return data.getBody()==null ? Result.error("无数据") : Result.success(data);
      }
}
}

